<?php
header("Content-type:text/html;charset=utf-8");
$sure = $_GET["sure"];
if($sure!="1")
{
	echo '<script language="JavaScript">;alert("对不起你需要在后台操作！");location.href="/panels";</script>;';
	exit();
}

session_start();
$userid=$_SESSION['account'];
$app = [];
$app['config'] = require 'config.php'; 
require 'Core/DataBase/ConnetDB.php';
require 'Core/DataBase/RequestDB.php';
require 'Request/VideoRequest.php';

$pdo = ConnetDB::make($app['config']['database']);
$quesy = new RequestDB($pdo);
$quesyvideo = new VideoRequest($pdo);

$isadmins = $quesy -> isAdmins($userid);

if($isadmins[0][0] != "1")
{
	echo '<script language="JavaScript">;alert("Sorry you no have permission to operate!");location.href="/";</script>;';
	exit();
}

  $filevideo=getcwd()."/video/";
  $filevideoimg=getcwd()."/videoimg/";
  $filesound=getcwd()."/WebView/Static/Img";
  //var_dump($filevideo);
  $operlocal = "tempvideo/"; //视频目录
  $oper = "video/"; //要存放的目录
  $statement = null;
  $handle = opendir('tempvideo'); //当前目录
  
  while (false !== ($file = readdir($handle))) { //遍历该php文件所在目录
	$kzm = substr(strrchr($file, '.'), 1);
	$filesname = str_replace(strrchr($file, "."),"",$file); 
  
    if($kzm=="mp4") { //文件过滤
	 
	 $statement = $pdo ->prepare("select * from videos where isdel=1");
	 $statement -> execute();
	 $arraydelids = $statement -> fetchAll();
	 if($arraydelids!=null){
	 	$arryids = $arraydelids[0][0];
	 }else{
	 	$arryids = null;
	 }
	 
	 if($arryids){
	   
	 	$temprand1 = MakeVKey();
	 	$tempisname1 = $oper.$temprand1.".".$kzm;
		copy($filesound."/sound.png",$filevideoimg.$temprand1.'.png');
	 	rename($operlocal.$file, $tempisname1);
	 	$statement = $pdo ->prepare("UPDATE `videos` SET `name`='$filesname', `vid`='$temprand1' , `isdel`='0' WHERE  `id`='$arryids'");
	 	$statement -> execute();
	 
	 }else{
	 	$temprand = MakeVKey();
	 	$tempisname = $oper.$temprand.".".$kzm;
		copy($filesound."/sound.png",$filevideoimg.$temprand.'.png');
	 	rename($operlocal.$file, $tempisname);
	 	$statement = $pdo ->prepare("insert videos(name,vid) values('$filesname','$temprand')");
	 	$statement -> execute();
	 }
	 
    }elseif($kzm=="mp3"){
		
		$statement = $pdo ->prepare("select * from videos where isdel=1");
		$statement -> execute();
		$arraydelids = $statement -> fetchAll();
		if($arraydelids!=null){
			$arryids = $arraydelids[0][0];
		}else{
			$arryids = null;
		}
		
		if($arryids){
		  
			$temprand1 = MakeVKey();
			$tempisname1 = $oper.$temprand1.".mp4";
			copy($filesound."/sound.png",$filevideoimg.$temprand1.'.png');
			rename($operlocal.$file, $tempisname1);
			$statement = $pdo ->prepare("UPDATE `videos` SET `name`='$filesname', `vid`='$temprand1' , `isdel`='0' WHERE  `id`='$arryids'");
			$statement -> execute();
		
		}else{
			$temprand = MakeVKey();
			$tempisname = $oper.$temprand.".mp4";
			copy($filesound."/sound.png",$filevideoimg.$temprand.'.png');
			rename($operlocal.$file, $tempisname);
			$statement = $pdo ->prepare("insert videos(name,vid) values('$filesname','$temprand')");
			$statement -> execute();
		}
	}
	
  }
  
  
  //函数随机VID
  function MakeVKey() {
    $VKey = substr(sha1(microtime(true)), 0, 12);
  	return $VKey;
  }
  
  if($statement == null){
	  echo "执行目录tempvideo内没有任何的视频!";
  }else{
	  if($statement -> rowCount() > 0){
	      echo "导入目录tempvideo视频成功!";
	   }else{
	        echo "执行目录tempvideo导入失败!";
	  	}
  }
?>